[[...path]].page.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React from 'react';
  2. import { IUser, IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  9. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  10. import { Page } from '~/components/Page';
  11. import { CrowiRequest } from '~/interfaces/crowi-request';
  12. import { RendererConfig } from '~/interfaces/services/renderer';
  13. import { IShareLinkHasId } from '~/interfaces/share-link';
  14. import {
  15. useCurrentUser, useCurrentPagePath, useCurrentPathname, useCurrentPageId, useRendererConfig,
  16. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault,
  17. } from '~/stores/context';
  18. import {
  19. CommonProps, getServerSideCommonProps, useCustomTitle, getNextI18NextConfig,
  20. } from '../utils/commons';
  21. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  22. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  23. type Props = CommonProps & {
  24. shareLink?: IShareLinkHasId,
  25. isExpired: boolean,
  26. currentUser: IUser,
  27. disableLinkSharing: boolean,
  28. isSearchServiceConfigured: boolean,
  29. isSearchServiceReachable: boolean,
  30. isSearchScopeChildrenAsDefault: boolean,
  31. rendererConfig: RendererConfig,
  32. };
  33. const SharedPage: NextPage<Props> = (props: Props) => {
  34. useShareLinkId(props.shareLink?._id);
  35. useCurrentPageId(props.shareLink?.relatedPage._id);
  36. useCurrentPagePath(props.shareLink?.relatedPage.path);
  37. useCurrentUser(props.currentUser);
  38. useCurrentPathname(props.currentPathname);
  39. useRendererConfig(props.rendererConfig);
  40. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  41. useIsSearchServiceReachable(props.isSearchServiceReachable);
  42. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  43. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  44. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  45. return (
  46. <ShareLinkLayout title={useCustomTitle(props, 'GROWI')} expandContainer={props.isContainerFluid}>
  47. <div className="h-100 d-flex flex-column justify-content-between">
  48. <header className="py-0 position-relative">
  49. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  50. </header>
  51. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  52. <div className="flex-grow-1">
  53. <div id="content-main" className="content-main grw-container-convertible">
  54. { props.disableLinkSharing && (
  55. <div className="mt-4">
  56. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  57. </div>
  58. )}
  59. { (isNotFound && !props.disableLinkSharing) && (
  60. <div className="container-lg">
  61. <h2 className="text-muted mt-4">
  62. <i className="icon-ban" aria-hidden="true" />
  63. <span> Page is not found</span>
  64. </h2>
  65. </div>
  66. )}
  67. { (props.isExpired && !props.disableLinkSharing) && (
  68. <div className="container-lg">
  69. <h2 className="text-muted mt-4">
  70. <i className="icon-ban" aria-hidden="true" />
  71. <span> Page is expired</span>
  72. </h2>
  73. </div>
  74. )}
  75. {(isShowSharedPage && props.shareLink != null) && (
  76. <>
  77. <ShareLinkAlert expiredAt={props.shareLink.expiredAt} createdAt={props.shareLink.createdAt} />
  78. <Page />
  79. </>
  80. )}
  81. </div>
  82. </div>
  83. </div>
  84. </ShareLinkLayout>
  85. );
  86. };
  87. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  88. const req: CrowiRequest = context.req as CrowiRequest;
  89. const { crowi } = req;
  90. props.disableLinkSharing = crowi.configManager.getConfig('crowi', 'security:disableLinkSharing');
  91. props.isSearchServiceConfigured = crowi.searchService.isConfigured;
  92. props.isSearchServiceReachable = crowi.searchService.isReachable;
  93. props.isSearchScopeChildrenAsDefault = crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  94. props.rendererConfig = {
  95. isEnabledLinebreaks: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  96. isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  97. adminPreferredIndentSize: crowi.configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  98. isIndentSizeForced: crowi.configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  99. plantumlUri: process.env.PLANTUML_URI ?? null,
  100. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  101. // XSS Options
  102. isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  103. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  104. tagWhiteList: crowi.xssService.getTagWhiteList(),
  105. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  106. };
  107. }
  108. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  109. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  110. props._nextI18Next = nextI18NextConfig._nextI18Next;
  111. }
  112. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  113. const req = context.req as CrowiRequest<IUserHasId & any>;
  114. const { user, crowi } = req;
  115. const result = await getServerSideCommonProps(context);
  116. if (!('props' in result)) {
  117. throw new Error('invalid getSSP result');
  118. }
  119. const props: Props = result.props as Props;
  120. if (user != null) {
  121. props.currentUser = user.toObject();
  122. }
  123. const { linkId } = req.params;
  124. try {
  125. const ShareLinkModel = crowi.model('ShareLink');
  126. const shareLink = await ShareLinkModel.findOne({ _id: linkId }).populate('relatedPage');
  127. if (shareLink != null) {
  128. props.isExpired = shareLink.isExpired();
  129. props.shareLink = shareLink.toObject();
  130. }
  131. }
  132. catch (err) {
  133. //
  134. }
  135. injectServerConfigurations(context, props);
  136. // await injectUserUISettings(context, props);
  137. await injectNextI18NextConfigurations(context, props);
  138. return {
  139. props,
  140. };
  141. };
  142. export default SharedPage;